package com.keller.controller;

import com.keller.domian.Permission;
import com.keller.domian.Role;
import com.keller.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @RequestMapping("/findAll")
    public ModelAndView findAllRoles(){
        ModelAndView mv = new ModelAndView();
        List<Role> roleList = roleService.findAllRoles();
        mv.addObject("roleList",roleList);
        mv.setViewName("role-list");
        return mv;
    }

    @RequestMapping("/save")
    public String saveRole(Role role){
        roleService.saveRole(role);
        return "redirect:findAll.do";
    }

    //根据角色id查找角色及角色没有的权限
    @RequestMapping("/findRoleByIdAndAllPermission")
    public ModelAndView findRoleByIdAndAllPermission(String id){
        ModelAndView mv = new ModelAndView();
        //根据角色id查询角色
        Role role = roleService.findRoleById(id);
        //根据角色id查询角色没有的权限
        List<Permission> otherPermission = roleService.findOtherPermissionByRoleId(id);
        mv.addObject("permissionList",otherPermission);
        mv.addObject("role",role);
        mv.setViewName("role-permission-add");
        return mv;
    }

    //将权限添加到角色
    @RequestMapping("/addPermissionToRole")
    public String addPermissionToRole(String roleId, String[] ids){
        roleService.addPermissionToRole(roleId, ids);
        return "redirect:findAll.do";
    }
}
